package com.example.controller;

import com.example.common.Result;
import com.example.entity.Animal;
import com.example.entity.User;
import com.example.entity.Volunteer;
import com.example.service.AdminService;
import com.example.service.AnimalService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 宠物信息模块前端请求的端口
 */
@RestController
@RequestMapping("/animal")
public class AnimalController {

    @Resource
    private AnimalService animalService;

/*
* 新增
*
*
* */

    @PostMapping("/add")
    public Result add(@RequestBody  Animal animal){
        animalService.add(animal);

     return Result.success();
    }

    /*
    * 更新
    * */
    @PutMapping("/update")
    public Result update(@RequestBody Animal animal) {
        animalService.updateById(animal);
        return Result.success();
    }

    /*
     * 删除
     * */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        animalService.deleteById(id);
        return Result.success();
    }

/**
 *
 * 分页查询
 */
@GetMapping("/selectPage")
public Result selectPage(Animal animal,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "5") Integer pageSize) {
    PageInfo<Animal> pageInfo = animalService.selectPage(animal, pageNum, pageSize);
    return Result.success(pageInfo);
}
/**
 *
 * 查询所有
 */
@GetMapping("/selectAll")
    public Result selectAll(){
        List<Animal> list=animalService.selectAll();
        return Result.success(list);

    }


    /*
     * 个人查询
     *
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Animal animal =  animalService.selectById(id);
        return Result.success(animal);
    }

    /*
     * 更新用户宠物ID
     * */
    @PutMapping("/updateState")
    public Result updatePetssId(@RequestBody Animal animal) {
        if (animal != null ) {
            animalService.updateState(animal.getId(), animal.getState());
            return Result.success();
        } else {
            return Result.error("用户不存在");
        }
    }
  
}
